package com.himiotech.core.project.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.himiotech.core.project.data.mapper.OperateLogMapper;
import com.himiotech.core.project.model.entity.OperateLog;
import com.himiotech.core.project.model.metadata.OperateLogMetadata;
import com.himiotech.core.project.service.IOperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements IOperateLogService {

    @Autowired
    private OperateLogMapper operateLogMapper;


    /**
     * 查询分页数据
     *
     * @param page       分页信息
     * @param operateLog 业务信息查询条件
     * @return 分页对象及数据
     */
    @Override
    public Page<OperateLog> selectOperateLogListPage(Page<OperateLog> page, OperateLog operateLog) {
        QueryWrapper<OperateLog> query = Wrappers.query();
        query.eq(ObjectUtil.isNotEmpty(operateLog.getTitle()), OperateLogMetadata.TITLE.getColName(), operateLog.getTitle());
        query.eq(ObjectUtil.isNotEmpty(operateLog.getOperateType()), OperateLogMetadata.OPERATE_TYPE.getColName(), operateLog.getOperateType());
        if (ArrayUtil.isNotEmpty(operateLog.getBusinessTypes())) {
            query.in(
                    OperateLogMetadata.BUSINESS_TYPE.getColName(),
                    Arrays.stream(operateLog.getBusinessTypes()).collect(Collectors.toList()));
        }
        query.eq(operateLog.getBusinessType() != null, OperateLogMetadata.BUSINESS_TYPE.getColName(), operateLog.getBusinessType());
        query.like(ObjectUtil.isNotEmpty(operateLog.getOperateName()), OperateLogMetadata.OPERATE_NAME.getColName(), operateLog.getOperateName());
        query.like(ObjectUtil.isNotEmpty(operateLog.getDeptName()), OperateLogMetadata.DEPT_NAME.getColName(), operateLog.getDeptName());
        query.eq(ObjectUtil.isNotEmpty(operateLog.getStatus()), OperateLogMetadata.STATUS.getColName(), operateLog.getStatus());
        Map<String, Object> searchParams = operateLog.getSearchParams();
        if (!searchParams.isEmpty()) {
            String operateTimeStart = MapUtil.getStr(searchParams, StrUtil.toCamelCase(OperateLogMetadata.OPERATE_TIME.getColName()).concat("Start")).concat(" 00:00:00");
            query.ge(StrUtil.isNotEmpty(operateTimeStart), OperateLogMetadata.OPERATE_TIME.getColName(), operateTimeStart);
            String operateTimeEnd = MapUtil.getStr(searchParams, StrUtil.toCamelCase(OperateLogMetadata.OPERATE_TIME.getColName()).concat("End")).concat(" 23:59:59");
            query.le(StrUtil.isNotEmpty(operateTimeEnd), OperateLogMetadata.OPERATE_TIME.getColName(), operateTimeEnd);
        }

        page = operateLogMapper.selectPage(page, query);
        return page;
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperateLog() {
        operateLogMapper.cleanOperateLog();
    }
}
